//
//  MXBarVC.swift
//  XMPPTest
//
//  Created by 陈学明 on 17/2/9.
//  Copyright © 2017年 muxue. All rights reserved.
//

import UIKit

class MXBarVC: UITabBarController {

    lazy var barBttoms = [Any]()
//    lazy var barLabels = [Any]()
    
    
    lazy var barView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
    var selectBtn:UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
    }

    private func createUI(){
        let barItmeTile:[[String:String]] = [["title":"聊天","imageName":"","VCClass":"LXChatRoomManageVC"],["title":"好友","imageName":"","VCClass":"MXFriendslListVC"]]
        var vcs = [UIViewController]();
        
        for dict in barItmeTile {
            vcs.append(createBarItmeReturn(dict: dict))
        }
        viewControllers = vcs
        tabBar.addSubview(barView)
    }
   private func createBarItmeReturn(dict:[String:String]) -> UIViewController {
        guard let title = dict["title"] ,
            let imageName = dict["imageName"],
            let clasName = dict["VCClass"],
            let VCClass = NSClassFromString(Bundle.main.namesparce() + "." + clasName) as? UIViewController.Type
            else {
                return UIViewController();
        }
        let barItme = UIButton(type: .custom)
        
        barItme.setImage(UIImage(named:imageName), for: UIControlState(rawValue:0))
        barItme.setImage(UIImage(named:imageName), for: .highlighted);
        barItme.setTitle(title, for: UIControlState(rawValue:0));
        barItme.setTitleColor(UIColor.black, for: UIControlState(rawValue:0))
        barItme.setTitleColor(UIColor.orange, for: .selected)
        barView.addSubview(barItme)
        barItme.tag = barBttoms.count

        barItme.rac_signal(for: .touchUpInside).subscribeNext { x in
            self.selectBtn?.isSelected = false
            self.selectBtn = barItme;
            self.selectBtn?.isSelected = true
            self.selectedIndex = (self.selectBtn?.tag)!
        }
        
        barItme.sd_layout()
        .leftSpaceToView(barView,CGFloat(barBttoms.count)*(UIScreen.main.bounds.size.width/2.0))?
        .topSpaceToView(barView,0)?
        .bottomSpaceToView(barView,0)?
        .widthRatioToView(barView,0.5);
        if barBttoms.count==0 {
            self.selectBtn = barItme
            barItme.isSelected = true;
        }
        barBttoms.append(barItme)
        

        return MXNavVC(rootViewController: VCClass.init());
    }
}

extension Bundle{
    //返回命命空间字符串
    func namesparce() -> String {
        return Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""
    }
}

protocol RentDelegate {
    //协议内容和方法
    func rentdelegateName(name:String)
}
